﻿CREATE PROCEDURE Clientes_DevolverPorCodigo

@Codigo VARCHAR(10),
@IncluirImagen BIT

AS

IF(@IncluirImagen = 1)
BEGIN
	SELECT c.IdCliente, 
		c.IdRubroCliente, 
		c.IdTipoIva, 
		c.IdListaPrecioVenta, 
		c.IdVendedor, 
		c.Codigo, 
		c.Nombre, 
		c.LimiteDeuda, 
		c.Cuit, 
		c.Email, 
		c.SitioWeb, 
		c.Observaciones, 
		c.Imagen, 
		c.EstaEliminado
		FROM Clientes c
		WHERE c.Codigo = @Codigo
END
ELSE
BEGIN
	SELECT c.IdCliente, 
		c.IdRubroCliente, 
		c.IdTipoIva, 
		c.IdListaPrecioVenta, 
		c.IdVendedor, 
		c.Codigo, 
		c.Nombre, 
		c.LimiteDeuda, 
		c.Cuit, 
		c.Email, 
		c.SitioWeb, 
		c.Observaciones, 
		CASE WHEN c.Imagen IS NULL
			THEN NULL
			ELSE CONVERT(IMAGE, 0x) END AS Imagen,
		c.EstaEliminado
		FROM Clientes c
		WHERE c.Codigo = @Codigo
END

SELECT ctfp.IdCliente,
	ctfp.IdTipoFormaPago
	FROM ClientesTiposFormasPagos ctfp
		INNER JOIN Clientes c
			ON ctfp.IdCliente = c.IdCliente
	WHERE c.Codigo = @Codigo

SELECT dc.IdDomicilioCliente, 
	dc.IdCliente, 
	dc.IdTipoDomicilio, 
	dc.IdBarrio, 
	dc.Calle, 
	dc.Numero, 
	dc.Piso, 
	dc.Departamento
	FROM DomiciliosClientes dc
		INNER JOIN Clientes c
			ON dc.IdCliente = c.IdCliente
	WHERE c.Codigo = @Codigo

SELECT tc.IdTelefonoCliente, 
	tc.IdTipoTelefono, 
	tc.IdCliente, 
	tc.CodigoArea, 
	tc.Numero
	FROM TelefonosClientes tc
		INNER JOIN Clientes c
			ON tc.IdCliente = c.IdCliente
	WHERE c.Codigo = @Codigo

SELECT ccpc.IdCliente,
	ccpc.IdCampoPersonalizadoCliente,
	ccpc.Valor
	FROM ClientesCamposPersonalizadosClientes ccpc
		INNER JOIN Clientes c
			ON ccpc.IdCliente = c.IdCliente
	WHERE c.Codigo = @Codigo